lzyy = {};
lzyy.br2nl = function(str)
{
	return str.replace(/<br\s*\/?>/mg,"\n");
};
lzyy.nl2br = function(str)
{
	return str.replace(/\n/mg,"<br />");
};
lzyy.post_to_url = function(path, params, method) 
{
    method = method || "post";
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

	for(var key in params) 
	{
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);    // Not entirely sure if this is necessary
    form.submit();
};
lzyy.ajax_submit = function($dest)
{
	var $err_msg = $('div.error_message',$dest);
	if(!$err_msg.length)
	{
		$err_msg = $('div.error_message',$dest.parent());
	}
	$err_msg.text('');
	var _url,_data;
	if($dest.attr('href')!=undefined)
	{
		alert($dest.attr('href'));
		_url = $dest.attr('href');
		_data = '';
	}
	else
	{
		_url = $dest.attr('action');
		_data = $dest.serializeArray();
	}
	$.ajax({
		type:'post',
		url:_url,
		dataType:'json',
		data:_data,
		success:function(data)
		{
			$dest.find('input[type="submit"]').removeAttr('disabled');
			if(data.status == 'success')
			{
				if(typeof data.msg.url != 'undefined')
					location.href=data.msg.url;
			}
			else
			{
				for(var i in data.msg)
				{
					$('div[rel="'+i+'"]',$dest).text(data.msg[i]);
				}
			}
		},
		error:function()
		{
			$dest.find('input[type="submit"]').removeAttr('disabled');
			alert('something wrong happened,your post was not submited');
		}
	});
};

jQuery.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'gray';
  }
 
  return this.each(function () {
    // get jQuery version of 'this'
    var $input = jQuery(this),
 
    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = jQuery(this.form),
      $win = jQuery(window);
 
    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }
 
    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title
 
      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};
 
$(function(){
	$('input[title!=""],textarea[title!=""]').hint('gray');
	$(document).bind('click',function(e){
		if(!$(e.target).hasClass('dropdown'))
		{
			$('.dropdown ul').slideUp(100);
		}
	});
	$('.dropdown').bind('click',function(e){
		e.preventDefault();
		var $self = $(this);
		$self.find('ul').slideToggle(100);
		if( e.stopPropagation ) { e.stopPropagation(); } //For 'Good' browsers
		else { e.cancelBubble = true; } //For IE
	});
	$('#publish_feedback .dropdown li').bind('click',function(e){
		var $self = $(this);
		var value = $self.text();
		var $p = $self.parent().prev();
		$p.html(value+' <span style="font-size:10px">▼ </span>');
		$('input[name="type"]').val($self.attr('value'));
		$self.parent().slideUp(100);
		if( e.stopPropagation ) { e.stopPropagation(); } //For 'Good' browsers
		else { e.cancelBubble = true; } //For IE
	});
	$('table .dropdown li').bind('click',function(e){
		if( e.stopPropagation ) { e.stopPropagation(); } //For 'Good' browsers
		else { e.cancelBubble = true; } //For IE
		location.href = $(this).find('a').attr('href');
	});
	$('#login_btn').bind('click',function(e){
		var $self = $(this);
		$self.parent().hide();
		$self.parent().next().show();
	});

});
